/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parser;

/**

 @author FiLiP
 */
public class LexicalAnalyzer
{
    private String input;
    private int offset;
	private int prevOffset;

    public LexicalAnalyzer(String inputText)
    {
		offset = 0;
		prevOffset = 0;
        input = inputText;
    }

    public String getNextToken()
    {
        if (offset >= input.length())
        {
            return "";
        }
		prevOffset = offset;
        char ch = input.charAt(offset);

        switch (ch)
        {
            case '<':
                if (offset + 1 < input.length() && input.charAt(offset + 1) == '/')
                {
                    offset += 2;
                    return "</";

                }
                else if (offset + 14 < input.length() && input.substring(offset, offset + 14).compareTo("<?xml version=") == 0)
                {
                    offset += 14;
                    return "<?xml version=";
                }
                break;

            case '/':
                if (offset + 1 < input.length() && input.charAt(offset + 1) == '>')
                {
                    offset += 2;
                    return "/>";
                }
                break;

            case '?':
                if (offset + 1 < input.length() && input.charAt(offset + 1) == '>')
                {
                    offset += 2;
                    return "?>";
                }
                break;
        }
        offset++;
        
        if (Character.isLetter(ch))
        {
            return "letter";
        }
        
        if (Character.isDigit(ch))
        {
            return "digit";
        }
        
        return String.valueOf(ch);
    }

	public int getPrevOffset()
	{
		return prevOffset;
	}
	
	public int getOffset()
	{
		return offset;
	}
}
